/**
 * 
 */
package com.soap;


import java.io.IOException;
import java.net.SocketTimeoutException;
import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Log;
import com.clases.Categoria;


/**
 * @author Chris
 * 
 */
public class Soap_Categoria {

    private static String TAG = "SOAP LUGAR";

    SoapSerializationEnvelope envelope;

    PropertyInfo pi;

    SoapObject Request;

    public Categoria[] getCategorias() {

        String METHOD_NAME = "getcategorias";
        String SOAP_ACTION = SoapUtil.NAMESPACE + METHOD_NAME;

        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);

        Object response = null;

        try {

            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();

            return getInformacion(response);

        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", x.getMessage());

            Categoria[] lugs = new Categoria[1];
            Categoria lk = new Categoria();
            lk.setIDCategoria(-10);
            lugs[0] = lk;
            return lugs;
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String x = " ";
            for (StackTraceElement line : stack) {
                x += line.toString();
            }
            Log.e(TAG + ": ", x);

            Categoria[] lugs = new Categoria[1];
            Categoria lk = new Categoria();
            lk.setIDCategoria(-10);
            lk.setNombreCategoria(x);
            lugs[0] = lk;
            return lugs;

        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());

            Categoria[] lugs = new Categoria[1];
            Categoria lk = new Categoria();
            lk.setIDCategoria(-10);
            lk.setNombreCategoria(x.getMessage());
            lugs[0] = lk;
            return lugs;
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());

            Categoria[] lugs = new Categoria[1];
            Categoria lk = new Categoria();
            lk.setIDCategoria(-10);
            lk.setNombreCategoria(x.getMessage());
            lugs[0] = lk;
            return lugs;
        }
    }

    private Categoria[] getInformacion(Object soap) {

        String RES = soap.toString();
        try {
            JSONArray jsonArray = new JSONArray(RES);
            Categoria[] cats = new Categoria[jsonArray.length()];
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jo = new JSONObject(jsonArray.getString(i));
                int IDCategoria = jo.getInt("IDCategoria");
                String NombreCategria = jo.getString("NombreCategoria");

                Categoria cat = new Categoria();
                cat.setIDCategoria(IDCategoria);
                cat.setNombreCategoria(NombreCategria);

                cats[i] = cat;

            }

            return cats;
        }
        catch (Exception x) {

            Categoria[] cats = new Categoria[1];
            Categoria cat = new Categoria();
            cat.setNombreCategoria(x.getMessage());
            cats[0] = cat;
            return cats;

        }
    }
}
